﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAL;

namespace 包覆门.GlassManager
{
    public partial class GlassList : Form
    {
        public GlassList()
        {
            InitializeComponent();
            this.dgGlass.AutoGenerateColumns = false;
        }
        private GlassDAL dal = new GlassDAL();
        private Dictionary<int, string> dic = new Dictionary<int, string>();

        private void GlassList_Load(object sender, EventArgs e)
        {
            dic.Add(0, "否");
            dic.Add(1, "是");
            BindingSource bs = new BindingSource();
            bs.DataSource = dic;
            this.isspecial.DataSource = bs;
            this.isspecial.DisplayMember = "value";
            this.isspecial.ValueMember = "key";
            LoadData();
        }
        //加载数据
        public void LoadData() {
            string glass = this.tbGlass.Text.Trim();


            this.dgGlass.DataSource = dal.GlassListAll(glass);
        }
        //新增
        private void tsbAdd_Click(object sender, EventArgs e)
        {
            GlassEditing ge = new GlassEditing();
            ge.IsNew = true;
            if (ge.ShowDialog() == DialogResult.OK) {
                LoadData();
            }
        }
        //编辑
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            int count = this.dgGlass.SelectedRows.Count;
            if (count <= 0) {
                MessageBox.Show("请选中要编辑的行");
                return;
            }
            GlassEditing ge = new GlassEditing();
            ge.IsNew = false;
            ge.EditingId = this.dgGlass.SelectedRows[0].Cells["gid"].Value.ToString();
            if (ge.ShowDialog() == DialogResult.OK) {
                LoadData();
            }
        }
        //删除
        private void tsbDel_Click(object sender, EventArgs e)
        {
            int count = this.dgGlass.SelectedRows.Count;
            if (count <= 0)
            {
                MessageBox.Show("请选择要删除的行");
                return;
            }
            string info = this.dgGlass.SelectedRows[0].Cells["glasstype"].Value.ToString();
            if (MessageBox.Show("确认要删除<" + info + ">么", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                int result = dal.DelByGid(this.dgGlass.SelectedRows[0].Cells["gid"].Value.ToString());
                if (result == 1)
                {
                    MessageBox.Show("删除成功");
                    LoadData();
                }
                else
                {
                    MessageBox.Show("删除失败");
                }
            }
        }
        //关闭
        private void tsbClose_Click(object sender, EventArgs e)
        {
            
            Close();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
